package com.jarvis.pzz;


import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.webkit.WebResourceRequest;
import android.webkit.WebResourceResponse;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jarvis.pzz.common.BaseActivity;
import com.jarvis.pzz.util.VersionManager;

import butterknife.BindView;

public class WebViewActivity extends BaseActivity {
    @BindView(R.id.webView)
    WebView webView;
    @BindView(R.id.rel_left)
    RelativeLayout rel_left;
    @BindView(R.id.rel_right)
    RelativeLayout rel_right;
    @BindView(R.id.tv_value)
    TextView tv_value;

    private String url;
    private String type;
    @BindView(R.id.lin_title)
    RelativeLayout lin_title;
    @BindView(R.id.tv_title)
    TextView tv_title;
    @BindView(R.id.img_left)
    ImageView img_left;

    @Override
    public int getLayoutId() {
        return R.layout.activity_webview;
    }

    @Override
    public void initData() {
        VersionManager.getVersionName(this);

        WebSettings webSettings = webView.getSettings();
        webSettings.setUseWideViewPort(false);  //将图片调整到适合webview的大小

        webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN); //支持内容重新布局
        webSettings.setJavaScriptEnabled(true);
        if ("0".equals(type)) {
            webView.loadDataWithBaseURL(null, url, "text/html", "UTF-8", null);
        } else {
            webView.loadUrl(url);

        }

        webView.setWebViewClient(new WebViewClient() {

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                showDialog("loading");
            }

            @Override
            public void onReceivedHttpError(WebView view, WebResourceRequest request, WebResourceResponse errorResponse) {
                super.onReceivedHttpError(view, request, errorResponse);
                disMiss();
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                disMiss();
            }
        });
    }
    private void setBar() {
        lin_title.setVisibility(View.VISIBLE);
        lin_title.getBackground().setAlpha(255);
        int statusBarHeight1 = -1;
        //获取status_bar_height资源的ID
        int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            //根据资源ID获取响应的尺寸值
            statusBarHeight1 = getResources().getDimensionPixelSize(resourceId);
        }
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(lin_title.getLayoutParams());
        lp.height=lp.height+statusBarHeight1;
        //lp.setMargins(0, statusBarHeight1, 0, 0);
        lin_title.setLayoutParams(lp);
        tv_title.setPadding(0, statusBarHeight1, 0, 0);
        rel_left.setPadding(0, statusBarHeight1, 0, 0);
        img_left.setPadding(0, statusBarHeight1, 0, 0);
    }
    @Override
    public void initView() {
        setBar();
        Bundle bundle = this.getIntent().getExtras();
        String title = bundle.getString("title");
        url = bundle.getString("url");
        type = bundle.getString("type");
        if(TextUtils.isEmpty(title)){
            title="广告";
        }
        tv_title.setText(title);
        rel_right.setVisibility(View.GONE);
        rel_left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
}
